package com.inspector.app.ui.chat.socket;

import android.util.Log;

import com.example.commen.net.easyHttp.EasyHttpManager;
import com.example.commen.net.easyHttp.server.ReleaseServer;
import com.hjq.http.listener.HttpCallback;
import com.inspector.app.util.CommenArgs;

import java.io.File;
import java.util.List;

public class UploadFile {
    private UploadFileCallBack mUploadFileCallBack;

    public UploadFile setUploadFileCallBack(UploadFileCallBack uploadFileCallBack) {
        this.mUploadFileCallBack = uploadFileCallBack;
        return this;
    }


    public UploadFile uploadFile(File file) {
        String host = new ReleaseServer().getHost() + "/api/v1/oss/uploadToOSS";
        EasyHttpManager.get().uploadfile(host, file, CommenArgs.loginInfo.getToken() + "", new HttpCallback<List<EasyHttpManager.UpLoadFileResult.Data>>() {

            @Override
            public void onSucceed(List<EasyHttpManager.UpLoadFileResult.Data> result) {
                if (!result.isEmpty()) {
                    EasyHttpManager.UpLoadFileResult.Data data = result.get(0);
                    if (mUploadFileCallBack != null) {
                        mUploadFileCallBack.resultFile(data);
                    }
                } else {
                    if (mUploadFileCallBack != null) {
                        mUploadFileCallBack.error();
                    }
                }

            }

            @Override
            public void onFail(Exception e) {
                if (mUploadFileCallBack != null) {
                    mUploadFileCallBack.error();
                }
            }
        });
        return this;
    }

    public interface UploadFileCallBack {

        void resultFile(EasyHttpManager.UpLoadFileResult.Data data);

        void error();

    }
}
